/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.orm.bind;

import java.util.HashMap;
import java.util.Map.Entry;

import cc.aileron.commons.instance.InstanceManager;
import cc.aileron.orm.G2Dao;
import cc.aileron.orm.G2DaoManager;
import cc.aileron.orm.G2DaoPrivate;
import cc.aileron.orm.exception.SqlNotFoundRuntimeException;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Aileron
 * 
 */
@Singleton
public class G2DaoManagerImpl implements G2DaoManager
{
    @SuppressWarnings("unchecked")
    public <T> G2Dao<T> from(final Class<T> targetClass)
    {
        return (G2Dao<T>) this.daoMap.get(targetClass);
    }

    /**
     * @param provider
     * @param binder
     */
    @Inject
    public G2DaoManagerImpl(
        final InstanceManager provider,
        final G2DaoBinder binder)
    {
        for (final Entry<Class<?>, G2DaoPrivate<?>> e : binder.daoMap()
            .entrySet())
        {
            final G2DaoPrivate<?> dao = e.getValue();
            provider.injectMembers(dao);
            try
            {
                dao.initialize();
            }
            catch (final SqlNotFoundRuntimeException ee)
            {
            }
        }
        this.daoMap = binder.daoMap();
    }

    /**
     * map
     */
    private final HashMap<Class<?>, G2DaoPrivate<?>> daoMap;
}
